<template>
  <div>
    <!-- <el-button type="text" @click="modifyPw = true">修改密码</el-button> -->
    <el-dialog class="border-radius" title="修改密码" :visible.sync=" modifyPw" :show-close="false" width="80vw">
      <el-form :model="newPwForm" :rules="rules" ref="newPwForm">
        <el-form-item label="新密码" prop="newPwd">
          <el-input v-model="newPwForm.newPwd" autocomplete="off" placeholder="新密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPwd">
          <el-input v-model=" newPwForm.confirmPwd" placeholder="确认密码"> </el-input>
        </el-form-item>
      </el-form>
      <div>
        <el-row :gutter="90">
          <el-col :span="12">
            <el-button @click="change()" style="width: 80px; ">取 消</el-button>
          </el-col>
          <el-col :span="12">
            <el-button type="primary" @click="submitForm('newPwForm')" style="width: 80px;">确 定</el-button>
          </el-col>
        </el-row>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      let validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.newPwForm.newPwd !== '') {
            this.$refs.newPwForm.validateField('checkPass');
          }
          callback();
        }
      };
      let validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请确认密码'));
        } else if (value !== this.newPwForm.newPwd) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        modifyPw: true,
        name: 'Pop',
        newPwForm: {
          newPwd: '',
          confirmPwd: ''
        },
        rules: {
          newPwd: [{
            validator: validatePass,
            trigger: 'blur'
          }],
          confirmPwd: [{
            validator: validatePass2,
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {
      change(){
        this.modifyPw= false;
        this.$store.state.changepwd=0
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$postRequest({
              url: '/studentInfo/updateStuPwd',
              params: {
                'pwd': this.newPwForm.newPwd
              }
            }).then(res => {
              console.log(res);
              if (res.code == 200) {
                this.modifyPw = false;
                this.$alert('密码修改完成', '提示', {
                  confirmButtonText: '确定',
                  customClass: 'message_box_alert'
                })
                this.$store.state.changepwd=0
              }else{
                this.$alert(res.msg, '提示', {
                  confirmButtonText: '确定',
                  customClass: 'message_box_alert'
                })
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
        
      }
    }
  }
</script>

<style>
  .el-dialog {
    border-radius: 20px;
  }

  .message_box_alert {
    width: 80vw;
  }
</style>
